package org.harlov.testsys.views;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.harlov.testsys.db.ans_log;
import org.harlov.testsys.db.test_loger;
import org.harlov.testsys.db.users;
import org.harlov.testsys.views.add_forms.add_stud_form;

public class admin_form extends JFrame implements Runnable {

	private static final long serialVersionUID = 1L;
	public gui_factory gFactory;
	
	private JFrame mFrame; 
	
	public admin_form(String login ) throws SQLException {
		
		super("HTest|Admin Mode |"+login);
		gFactory = new gui_factory(this);
		
		
		mFrame = this;
		this.setPreferredSize(new Dimension(1200,600));
		this.setSize(1200,600);
		this.getContentPane().setLayout(null);
		this.setResizable(false);
		gui_factory.setCenterPosition(this);
		this.setVisible(true);
		gFactory.newComboBox("combo_sel_type", users.getTypes(),82, 25,new change_user_type());
		gFactory.newTable("tbl_studs", users.getUsersView(gFactory.getSelCombo("combo_sel_type")), 10, 50,500,400,new students_click());
		
		gFactory.setSelTable("tbl_studs", 0);
		
		gFactory.newList("tbl_logs", test_loger.getStudentsLogsView(users.getIdByUsersList(gFactory.getSelTable("tbl_studs")-1)), 520, 50,350,400,new logs_click());
		gFactory.newList("tbl_ans_logs", ans_log.getStudAnsView(test_loger.getLogIdByList(gFactory.getSelList("tbl_logs")-1)), 900,50,200,400,new ans_click(),new LocaleRenderer());
		gFactory.newLabel("Управление студентами","lbl_user_control", 10, 10,300,50);
		gFactory.newButton("Добавить студента", "b_add_student", 10, 500,180,40,new add_student());
		gFactory.newButton("Удалить студента", "b_remove_student", 210, 500,180,40, new remove_student());
		gFactory.newButton("Выход", "b_quit", 400, 500,180,40, new quit());
		
		
		
		
	}
	
	
	class students_click implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			test_loger.getStudentsLogsView(users.getIdByUsersList(gFactory.getSelTable("tbl_studs")-1));
			
			gFactory.CompRepaint("tbl_logs");
		}
		
	}
	
	class logs_click implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			ans_log.getStudAnsView(test_loger.getLogIdByList(gFactory.getSelList("tbl_logs")-1));
			gFactory.CompRepaint("tbl_logs");
		}
		
	}
	
	class ans_click implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	
	
	
	class add_student implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			new add_stud_form((admin_form) mFrame);			
		}
		
		
	}
	
	class change_user_type implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				users.getUsersView(gFactory.getSelCombo("combo_sel_type"));
				gFactory.CompRepaint("tbl_studs");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	
	class remove_student implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			JTable temp = (JTable) gFactory.getObject("tbl_studs");
			users.delUser((String)temp.getValueAt(temp.getSelectedRow(),1),gFactory.getSelCombo("combo_sel_type"));
			temp.repaint();			
		}
		
	}
	
	class quit implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			mFrame.dispose();
			System.exit(0);
		}
		
	}
	
	public class LocaleRenderer extends DefaultListCellRenderer {
	    
		  /** Creates a new instance of LocaleRenderer */
		  public LocaleRenderer() {
		  }
		    
		  public Component getListCellRendererComponent(JList list, 
		                                              Object value,
		                                              int index, boolean isSelected,
		                                              boolean cellHasFocus) {

		    super.getListCellRendererComponent(list, 
		                                       value, 
		                                       index, 
		                                       isSelected, 
		                                       cellHasFocus);
		    
		    String val = (String) value;
		    String[] param = val.split(" ");
		    if (param[1].compareTo("true")==0) {
		    	setText(param[0]);
		    	setBackground(Color.green);
		    }else
		    {
		    	setText(param[0]);
		    	setBackground(Color.red);
		    }
		    
		    
		    return this;
		  }
	}
	
}
